package com.cspg.design.ui.gtasks;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AppCompatActivity;

import com.cspg.design.MainActivity;
import com.cspg.design.R;
import com.cspg.design.dto.ProjectDto;
import com.cspg.design.presenter.ProjectPresenter;
import com.cspg.design.ui.map.MapActivity;
import com.cspg.design.ui.project.FlowActivity;
import com.cspg.design.ui.project.ProjectInfoActivity;
import com.cspg.design.ui.project.ProjectParameterActivity;
import com.cspg.design.ui.search.SearchActivity;
import com.cspg.design.utils.ActionBarUtil;
import com.cspg.design.utils.AppManager;
import com.cspg.design.utils.StatusBarUtil;
import com.cspg.design.widget.AlertListDialog;
import com.cspg.design.widget.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GTasksActivity extends AppCompatActivity {

    private ListView mListViewProject;
    private List<ProjectDto> projectDtos = new ArrayList<>();
    private ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
    private SimpleAdapter adapter;
    private ProjectPresenter projectPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_g_tasks);
        AppManager.getAppManager().addActivity(this);
        StatusBarUtil.setStatusBarColor(this, R.color.colorPrimary);
        ActionBarUtil.setActionBar(this, R.string.wait_work_title);
        mListViewProject = (ListView) findViewById(R.id.lv_gtasks_project);
        mListViewProject.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (projectPresenter != null && projectPresenter.getProjectDto().size() > position)
                {
                    ProjectDto projectDto = projectPresenter.getProjectDto().get(position);
                    choiceScheme(position, projectDto, false);
                }
                else
                {
                    ToastUtils.toast(GTasksActivity.this,"获取项目失败。。。");
                }
            }
        });
        LinearLayout searchLinearLayout = findViewById(R.id.lv_gtasks_search);
        searchLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GTasksActivity.this, SearchActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
            }
        });
        adapter = new SimpleAdapter(GTasksActivity.this, dataList, R.layout.listview_gtasks_project_item,
                new String[]{"title","name", "status"},
                new int[]{R.id.item_gtasks_project_title,R.id.item_gtasks_project_name, R.id.item_gtasks_project_status}
        );
        mListViewProject.setAdapter(adapter);
        projectPresenter = new ProjectPresenter(this,mListViewProject,adapter,dataList);
        projectPresenter.getProject(null);
        initData();
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    private void initData() {
//        Intent intent = getIntent();
//        if (intent != null)
//        {
//           Bundle bundle =  intent.getExtras();
//           if (bundle != null && bundle.get("project") != null)
//           {
//             this.projectDtos = (List<ProjectDto>)bundle.get("project");
//           }
//        }
//        refreshListView();
    }

    private void refreshListView() {
        dataList.clear();
        for (int i = 0; i < projectDtos.size(); i++) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("title", projectDtos.get(i).getProjectName());
            hashMap.put("name",projectDtos.get(i).getProjectID());
            hashMap.put("status", "待审核");
            dataList.add(hashMap);
        }
        adapter.notifyDataSetChanged();
    }

    //方案选择对话框
    private void choiceScheme(int position, final ProjectDto projectDto, boolean isChangeScheme) {
        //到审核界面
        new AlertListDialog.ListBuilder(this).setDividerColorRes(R.color.color_divider)
                .setDividerHeightRes(R.dimen.dp1)
                .setItems(new String[]{"评审","工程信息","流程信息","设备地图"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent;
                        switch (which)
                        {
                            case 0:
                                intent = new Intent(GTasksActivity.this, ProjectParameterActivity.class);
                                intent.putExtra("projectId", projectDto.getProjectID());
                                startActivity(intent);
                                break;
                            case 1:
                                intent = new Intent(GTasksActivity.this, ProjectInfoActivity.class);
                                intent.putExtra("projectId", projectDto.getProjectID());
                                startActivity(intent);
                                break;
                            case 2:
                                intent = new Intent(GTasksActivity.this, FlowActivity.class);
                                intent.putExtra("projectId", projectDto.getProjectID());
                                startActivity(intent);
                                break;
                            case 3:
                                intent = new Intent(GTasksActivity.this, MapActivity.class);
                                intent.putExtra("projectId", projectDto.getProjectID());
                                startActivity(intent);
                                break;
                        }
                    }
                }).create().show();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //两秒之内按返回键就会退出
            startActivity(new Intent(this, MainActivity.class));
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}